package com.ezjie.ielts.receiver;

import java.util.HashMap;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.ezjie.ielts.module_word.WordPracticeActivity;
import com.ezjie.ielts.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.umeng.analytics.MobclickAgent;

public class EZPushReceiver extends BroadcastReceiver {

	/**
	 * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView ==
	 * null)
	 */
	public static StringBuilder payloadData = new StringBuilder();

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		Log.d("GetuiSdkDemo", "onReceive() action=" + bundle.getInt("action"));

		switch (bundle.getInt(PushConsts.CMD_ACTION)) {
		case PushConsts.GET_MSG_DATA:
			// 获取透传数据
			// String appid = bundle.getString("appid");
			byte[] payload = bundle.getByteArray("payload");

			String taskid = bundle.getString("taskid");
			String messageid = bundle.getString("messageid");

			// smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
			boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
			System.out.println("第三方回执接口调用" + (result ? "成功" : "失败"));

			if (payload != null) {
				try {
                    String data = new String(payload);
                    Gson gson = new Gson();
                    
                    Map<String, Object> obj = gson.fromJson(data, new TypeToken<Map<String, Object>>() {}.getType());
                    String type = (String) obj.get("type");
                    LogUtils.d("summer", type+"type");
                    //友盟统计
                	Map<String, String> map = new HashMap<String, String>();
                	map.put("type", type);
                	MobclickAgent.onEvent(context, "getui_pushMsg", map);
//                	if ("1".equals(type)) {//抢考位推送
//                    	String monthOri = (String) obj.get("exam_time");
//                    	
//                    	// 处理月份和日期
//                    	String month = DateTimeUtil.formatDate(monthOri, "yyyy-MM-dd HH:mm:ss", "yyyy-MM");
//                    	String day = monthOri;
//                    	
//                    	String cityCode = (String) obj.get("province");
//                    	String cityName = (String) obj.get("province_name");
//                    	
//                    	PushUtil.addPushPrefValues(context, cityCode, cityName, month, day);
//                    	Intent actionIntent = BaseActivity.getStartIntent(context, R.layout.fragment_seat_detail_list);
//                    	actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    	context.startActivity(actionIntent);
//					}else if ("2".equals(type)) {//机经推送
//						Intent specialIntent = BaseActivity.getStartIntent(context, R.layout.fragment_gre_ranking);
//						specialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						context.startActivity(specialIntent);
//					}else if ("3".equals(type)) {//口语精华推送
//						String requestId = (String) obj.get("topid");
//						Intent wordIntent = BaseActivity.getStartIntent(context, R.layout.fragment_speak_first);
//						wordIntent.putExtra("requestId", requestId);
//						wordIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						context.startActivity(wordIntent);
//					}else 
					if ("4".equals(type)) {//单词学习提醒推送
						Intent wordIntent = new Intent(context,WordPracticeActivity.class);
						wordIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(wordIntent);
					}
//					else if ("5".equals(type)) {//主页
//						Intent wordIntent = new Intent(context,MainActivity2.class);
//						wordIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						context.startActivity(wordIntent);
//					}else if ("7".equals(type)) {//社区未读消息推送
//						LogUtils.d("summer", "收到社区推送了");
//						PreferencesUtil.putBoolean(context, "isHaveInfo", true);
//						EventBus.getDefault().post(new CommunityMessageEvent(true));
//					}
                } catch (Exception e) {
                    e.printStackTrace();
                }
			}
			
			break;

		case PushConsts.GET_CLIENTID:
			// 获取ClientID(CID)
			// 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
			String cid = bundle.getString("clientid");
			break;

		case PushConsts.THIRDPART_FEEDBACK:
			/*
			 * String appid = bundle.getString("appid"); String taskid =
			 * bundle.getString("taskid"); String actionid =
			 * bundle.getString("actionid"); String result =
			 * bundle.getString("result"); long timestamp =
			 * bundle.getLong("timestamp");
			 * 
			 * Log.d("GetuiSdkDemo", "appid = " + appid); Log.d("GetuiSdkDemo",
			 * "taskid = " + taskid); Log.d("GetuiSdkDemo", "actionid = " +
			 * actionid); Log.d("GetuiSdkDemo", "result = " + result);
			 * Log.d("GetuiSdkDemo", "timestamp = " + timestamp);
			 */
			break;

		default:
			break;
		}
	}
}
